package org.exez.modules.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.exez.common.util.DateUtils;
import org.exez.modules.system.util.VisitorBeanUtils;
import org.exez.modules.visitor.constant.PushStatusEnum;
import org.exez.modules.visitor.entity.Institution;
import org.exez.modules.visitor.entity.PushRecord;
import org.exez.modules.visitor.entity.ReservationApplication;
import org.exez.modules.visitor.mapper.PushRecordMapper;
import org.exez.modules.visitor.service.IInstitutionService;
import org.exez.modules.visitor.service.IPushRecordService;
import org.exez.modules.visitor.service.IReservationApplicationService;
import org.exez.modules.visitor.vo.PushRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 访客推送记录
 * @Author: exez-boot
 * @Date: 2020-06-28
 * @Version: V1.0
 */
@Service
public class PushRecordServiceImpl extends ServiceImpl<PushRecordMapper, PushRecord> implements IPushRecordService {

    @Autowired
    private IReservationApplicationService reservationApplicationService;

    @Autowired
    private IInstitutionService institutionService;
    @Override
    public Page<PushRecordVO> listPushRecord(Page<PushRecord> page, LambdaQueryWrapper<PushRecord> queryWrapper) {
        Page<PushRecordVO> pushRecordVOPage = new Page<>();
        List<PushRecordVO> pushRecordVOS = new ArrayList();

        IPage<PushRecord> pushRecordIPage = baseMapper.selectPage(page, queryWrapper);
        BeanUtils.copyProperties(pushRecordIPage, pushRecordVOPage);

        List<PushRecord> records = pushRecordIPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            pushRecordVOPage.setRecords(pushRecordVOS);
            return pushRecordVOPage;
        }

//        pushRecordVOS = VisitorBeanUtils.convertList2List(records, PushRecordVO.class);
//        List<Integer> collect = records.stream().map(PushRecord::getInstitutionId).collect(Collectors.toList());
//        LambdaQueryWrapper<Institution> institutionLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        institutionLambdaQueryWrapper.in(Institution :: getId,collect);
//        List<Institution> list = institutionService.list(institutionLambdaQueryWrapper);
//        list.
        for (PushRecordVO record : pushRecordVOS) {

            if (PushStatusEnum.YTS.getIndex().equals(record.getPushStatus())) {
                ReservationApplication byId = reservationApplicationService.getById(record.getReservationApplicationId());
                Institution institution = institutionService.getById(byId.getRespondentInstitutionId());
                if (institution != null){
                    record.setInstitutionName(institution.getInstitutionName());
                }
                String numberPlate= StringUtils.isBlank(byId.getNumberPlate()) ? "" : "，车牌号："+byId.getNumberPlate();
                record.setContent("授权"+byId.getName()+"成功"+numberPlate);
            }else if (PushStatusEnum.TSSB.getIndex().equals(record.getPushStatus())){
                record.setContent("授权失败");
            }

        }
        pushRecordVOPage.setRecords(pushRecordVOS);


        return pushRecordVOPage;
    }

    @Override
    public List<PushRecord> listPushRecordByStatusAll() {
        LambdaQueryWrapper<PushRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PushRecord::getPushStatus, 0, 2);
        queryWrapper.between(PushRecord::getVisitDate, DateUtils.getStartTime(),DateUtils.getEndTime());

        return baseMapper.selectList(queryWrapper);
    }
}
